package com.geek.novel.fragment

import android.view.View
import com.geek.novel.R
import com.geek.novel.activity.book.ViewerActivity
import com.geek.novel.ui.ReadView
import kotlinx.android.synthetic.main.layout_viewer_menu_setting_book.*

/**
 * 菜单、阅读页面的设置
 */
class FragmentReadSetting(private val sectionView: ReadView) : BaseFragment() {
	
	override fun layoutId(): Int = R.layout.layout_viewer_menu_setting_book
	override fun pageName(): String = "阅读设置"
	override fun fragmentName(): String = FragmentName
	
	override fun initV() {
		//显示字体大小的文字
		this.refreshReadViewTextSize()

		//显示字体行间距大小的文字
		this.refreshReadViewLineHeight()

		//设置音量键翻页、全屏翻页、禁止锁屏幕、自动翻页等功能
		this.initMenuKeyPageFunction()

		//绑定点击事件
		this.bindEvent()
	}

	private fun bindEvent(){
		//字体减小按钮事件
		setClickLsn(tv_read_menu_text_size_minus)
		//字体加大按钮事件
		setClickLsn(tv_read_menu_text_size_add)
		//字体行间距减小按钮事件
		setClickLsn(tv_read_menu_line_height_minus)
		//字体行间距加大按钮事件
		setClickLsn(tv_read_menu_line_height_add)
		//上一章按钮事件
		setClickLsn(btn_read_menu_prev_section)
		//下一章按钮事件
		setClickLsn(btn_read_menu_next_section)
		//翻页间隔时长-按钮事件
		setClickLsn(tv_turn_page_time_minus)
		//翻页间隔时长+按钮事件
		setClickLsn(tv_turn_page_time_add)
	}

	override fun myClick(v: View) {
		when(v.id){
			//字体减小按钮事件
			R.id.tv_read_menu_text_size_minus->{
				this.sectionView.textSizeMinus()
				this.refreshReadViewTextSize()
			}
			//字体加大按钮事件
			R.id.tv_read_menu_text_size_add->{
				this.sectionView.textSizeAdd()
				this.refreshReadViewTextSize()
			}
			//字体行间距减小按钮事件
			R.id.tv_read_menu_line_height_minus->{
				this.sectionView.textLineSizeMinus()
				this.refreshReadViewLineHeight()
			}
			//字体行间距加大按钮事件
			R.id.tv_read_menu_line_height_add->{
				this.sectionView.textLineSizeAdd()
				this.refreshReadViewLineHeight()
			}
			//上一章按钮事件
			R.id.btn_read_menu_prev_section->{
				this.sectionView.showPrevSection()
				this.initSectionTitle()
			}
			//下一章按钮事件
			R.id.btn_read_menu_next_section->{
				this.sectionView.showNextSection()
				this.initSectionTitle()
			}
			//翻页间隔时长-按钮事件
			R.id.tv_turn_page_time_minus->{
				this.sectionView.autoTurnPageTimeMinus()
				this.refreshAutoTurnPageTime()
			}
			//翻页间隔时长+按钮事件
			R.id.tv_turn_page_time_add->{
				this.sectionView.autoTurnPageTimeAdd()
				this.refreshAutoTurnPageTime()
			}
		}

	}

	/**设置音量键翻页、全屏翻页、禁止锁屏幕、自动翻页等功能*/
	private fun initMenuKeyPageFunction(){
		val mContext = context as ViewerActivity

		//全屏翻页
		switch_full_page_next.isChecked = sectionView.getFullScreenNextPage()
		switch_full_page_next.setOnCheckedChangeListener { _, isChecked ->
			sectionView.setFullScreenNextPage(isChecked)
		}
		//音量键翻页
		switch_voice_page.isChecked = mContext.getVoicePage()
		switch_voice_page.setOnCheckedChangeListener { _, isChecked ->
			mContext.setVoicePage(isChecked)
		}

		//禁止锁屏幕
		switch_lock_screen_never.isChecked = mContext.getLockScreenNever()
		switch_lock_screen_never.setOnCheckedChangeListener { _, isChecked ->
			mContext.setLockScreenNever(isChecked)
		}

		//自动翻页
		switch_auto_turn_page.isChecked = sectionView.getAutoTurnPage()
		initAutoTurnPage()
		switch_auto_turn_page.setOnCheckedChangeListener { _, isChecked ->
			sectionView.setAutoTurnPage(isChecked)
			initAutoTurnPage()
		}
	}

	/**显示自动翻页的时间选择控件*/
	private fun initAutoTurnPage(){
		val checked = switch_auto_turn_page.isChecked
		layout_turn_page_time_box.visibility = if(checked){
			View.VISIBLE
		}else{
			View.GONE
		}
		refreshAutoTurnPageTime()
	}


	/**显示自动翻页的时间大小的文字*/
	private fun refreshAutoTurnPageTime(){
		tv_turn_page_time.text = "${sectionView.getAutoTurnPageTime()}"
	}

	/**加载章节标题信息*/
	fun initSectionTitle(){
		val sectionTitle = sectionView.titles.joinToString("")
		text_read_menu_section_name.text = sectionTitle
	}


	/**显示字体大小的文字*/
	private fun refreshReadViewTextSize(){
		tv_read_menu_text_size.text = "${sectionView.contentTextSize}"
	}
	/**显示字体行间距大小的文字*/
	private fun refreshReadViewLineHeight(){
		tv_read_menu_line_height.text = "${sectionView.contentLineHeight}"
	}

	companion object {
		const val FragmentName = "FragmentMenuSetting"
		fun newInstance(sectionView: ReadView): FragmentReadSetting{
			return FragmentReadSetting(sectionView)
		}
	}
	
}
